package com.rytong.tools.ui;

import java.util.Timer;
import java.util.TimerTask;

import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.View;

public class LPLoadingView extends Component {
    Matrix MAT;
    Timer timer;
    private float degree_;
    public boolean isShowLoadingBg_;
    private Paint paint_;

    class MyLPLoadingView extends View implements CompositedComponent {

        public MyLPLoadingView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub

            if (null == MAT) {
                MAT = new Matrix();
            }
            if (null == timer) {
                timer = new Timer();
            }
        }

        @Override
        public Component composited() {
            // TODO Auto-generated method stub
            return LPLoadingView.this;
        }

        public void onDraw(Canvas canvas) {
            // 设置转圈的角度和中心点

            MAT.setRotate(degree_, focusImg_.getWidth() >> 1, focusImg_.getHeight() >> 1);
            MAT.postTranslate((width_ - focusImg_.getWidth()) >> 1,
                    (height_ - focusImg_.getHeight()) >> 1);
            degree_ = (float) (degree_ + 22.5);
            if (degree_ == 360)
                degree_ = 0;
            // 绘制背景图片
            if(isShowLoadingBg_){
                canvas.drawBitmap(unFocusImg_, 0, 0, paint_);
            }
            
            // 绘制进度旋转图片
            canvas.drawBitmap(focusImg_, MAT, paint_);
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    postInvalidate();
                }

            }, 100);
        }
    }

    public void mouldH() throws Exception {
        // 进度提示中间的圈圈图片宽度相对于320X480的界面定的大小
        final int wpp = 30;
        // 进度提示框背景宽度相对于320X480的界面定的大小
        final int wpb = 120;
        if (null == focusImg_) {
            // loading的转圈图片
            focusImg_ = Utils.getBitmapFromResources(getContext(), "progress1");
            final int w = Utils.getScaledValueX(wpp);
            final int h = w * focusImg_.getHeight() / focusImg_.getWidth();
            focusImg_ = Bitmap.createScaledBitmap(focusImg_, w, h, true);
        }
        if (null == unFocusImg_ && isShowLoadingBg_) {
            // loading的半透明灰色背景图
            unFocusImg_ = Utils.getBitmapFromResources(getContext(), "progressloadingb");
            unFocusImg_ = Bitmap.createScaledBitmap(unFocusImg_, width_, height_, true);
        }
    }

    public void initRealView(Activity activity, String nodeValue , boolean isShowLoadingBg) {
        realView_ = new MyLPLoadingView(activity);
        
        isShowLoadingBg_ = isShowLoadingBg;
        
        paint_ = new Paint();
        paint_.setAntiAlias(true);
        paint_.setColor(0xFFFFFFFF);
        paint_.setFilterBitmap(true);
    }
}
